<template>
	<view class="container">
		<!-- 页面头部 -->
		<view class="main-header"
			:style="{ height: platform == 'H5' ? '260rpx' : '320rpx', paddingTop: platform == 'H5' ? '0' : '0rpx' }">

			<!-- 用户信息 -->
			<view v-if="isLogin" class="user-info">
				<view class="user-avatar" @click="handlePersonal()">
					<avatar-image :url="userInfo.avatar_url" :width="100" />
				</view>
				<view class="user-content">
					<!-- 会员昵称 -->
					<view class="nick-name oneline-hide" @click="handlePersonal()">{{ userInfo.nick_name }}</view>
					<!-- 会员等级 -->
					<view v-if="userInfo.grade_id > 0 && userInfo.grade" class="user-grade">
						<view class="user-grade_icon">
							<image class="image"
								src="">
							</image>
						</view>
						<view class="user-grade_name">
							<text>{{ userInfo.grade.name }}</text>
						</view>
					</view>
					<!-- 会员无等级时显示手机号 -->
					<view v-else class="mobile">{{ userInfo.mobile }}</view>
				</view>
			</view>
			<!-- 未登录 -->
			<view v-else class="user-info" @click="handleLogin">
				<view class="user-avatar">
					<avatar-image :width="100" />
				</view>
				<view class="user-content">
					<view class="nick-name">未登录</view>
					<view class="login-tips">点击登录账号</view>
				</view>
			</view>
		</view>

		<!-- 订单操作 -->
		<view class="order-navbar">
			<block v-if="is_teacher==0">


				<view class="order-navbar-item" @click="onTargetLog()">
					<view class="item-icon">
						<image src="../../static/images/kaoshilog.png"></image>
					</view>
					<view class="item-name">考试记录</view>

				</view>
				<view class="order-navbar-item" @click="onTargetCert()">
					<view class="item-icon">
						<image src="../../static/images/zhengshu.png"></image>
					</view>
					<view class="item-name">生成证书</view>

				</view>

				<view class="order-navbar-item">
					<button class="btn-normal" open-type="contact">
						<view class="item-icon">
							<image src="../../static/images/kefu.png"></image>
						</view>
						<view class="item-name">联系客服</view>
					</button>


				</view>
			</block>
			<block v-else>
				<view class="order-navbar-item" @click="onTargetChouchalog()">
					<view class="item-icon">
						<image src="../../static/images/kaoshilog.png"></image>
					</view>
					<view class="item-name">抽查记录</view>

				</view>
				<view class="order-navbar-item">
					<button class="btn-normal" open-type="contact">
						<view class="item-icon">
							<image src="../../static/images/kefu.png"></image>
						</view>
						<view class="item-name">联系客服</view>
					</button>


				</view>
				<view class="order-navbar-item" @click="onTargetReset()">
					<view class="item-icon">
						<image src="../../static/images/reset.png"></image>
					</view>
					<view class="item-name">设置密码</view>

				</view>
			</block>



		</view>


		<!-- 退出登录 -->
		<view v-if="isLogin" class="my-logout">
			<view class="logout-btn" @click="handleLogout()">
				<text>退出登录</text>
			</view>
		</view>

		<!-- 与包裹页面所有内容的元素u-page同级，且在它的下方 -->
		<block v-if="is_teacher==0">
			<u-tabbar v-model="current" :list="list" active-color="#1684FC"></u-tabbar>
		</block>
		<block v-else>
			<u-tabbar v-model="current2" :list="teacherList" active-color="#1684FC" @change="changeTab"></u-tabbar>
		</block>


	</view>
</template>

<script>
	import store from '@/store'
	import AvatarImage from '@/components/avatar-image'
	import {
		setCartTabBadge
	} from '@/core/app'
	import SettingKeyEnum from '@/common/enum/setting/Key'
	import SettingModel from '@/common/model/Setting'
	import * as UserApi from '@/api/user'
	import * as OrderApi from '@/api/order'
	import {
		checkLogin
	} from '@/core/app'

	export default {
		components: {
			AvatarImage
		},
		data() {
			return {

				is_teacher: 0,

				// 枚举类
				SettingKeyEnum,
				// 正在加载
				isLoading: true,
				// 首次加载
				isFirstload: true,
				// 是否已登录
				isLogin: false,
				// 系统设置
				setting: {},
				// 当前用户信息
				userInfo: {},
				current: 0,
				current2: 1,
				list: [{
					iconPath: "https://i.antiedu.tech/static/tabbar/user.png",
					selectedIconPath: "https://i.antiedu.tech/static/tabbar/user-active.png",
					text: '我的',

					isDot: false,
					customIcon: false,
				}, ],

				teacherList: [{
						iconPath: "https://i.antiedu.tech/static/tabbar/home.png",
						selectedIconPath: "https://i.antiedu.tech/static/tabbar/home-active.png",
						text: '考试',
						isDot: false,
						customIcon: false,
					},
					{
						iconPath: "https://i.antiedu.tech/static/tabbar/user.png",
						selectedIconPath: "https://i.antiedu.tech/static/tabbar/user-active.png",
						text: '我的',

						isDot: false,
						customIcon: false,
					},
				],


			}
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow(options) {
			this.onRefreshPage()
		},

		methods: {

			changeTab(e) {


				if (e == 0) {

					uni.redirectTo({
						url: '/pages/ant/teacher/index'
					})
					// uni.reLaunch({
					// 	url:'/pages/ant/teacher/index'
					// })
				}

			},

			// 刷新页面
			onRefreshPage() {
				// 更新购物车角标
				setCartTabBadge()
				// 判断是否已登录
				this.isLogin = checkLogin()
				// 获取页面数据
				this.getPageData()
			},

			// 获取页面数据
			getPageData(callback) {
				const app = this
				app.isLoading = true
				Promise.all([app.getSetting(), app.getUserInfo(), app.getUserAssets(), app.getTodoCounts()])
					.then(result => {
						app.isFirstload = false
						// 初始化我的服务数据
						//app.initService()
						// 初始化订单操作数据
						//app.initOrderTabbar()
						// 执行回调函数
						callback && callback()
					})
					.catch(err => console.log('catch', err))
					.finally(() => app.isLoading = false)
			},

			// 初始化我的服务数据
			initService() {
				const app = this
				const newService = []
				service.forEach(item => {
					if (item.id === 'points') {
						item.name = '我的' + app.setting[SettingKeyEnum.POINTS.value].points_name
					}
					// 数据角标
					if (item.count != undefined) {
						item.count = app.todoCounts[item.id]
					}
					newService.push(item)
				})
				app.service = newService
			},

			// 初始化订单操作数据
			initOrderTabbar() {
				const app = this
				const newOrderNavbar = []
				orderNavbar.forEach(item => {
					if (item.count != undefined) {
						item.count = app.todoCounts[item.id]
					}
					newOrderNavbar.push(item)
				})
				app.orderNavbar = newOrderNavbar
			},

			// 获取商城设置
			getSetting() {
				const app = this
				return new Promise((resolve, reject) => {
					SettingModel.data()
						.then(setting => {
							app.setting = setting
							resolve(setting)
						}).catch(reject)
				})
			},

			// 获取当前用户信息
			getUserInfo() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : UserApi.info({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.userInfo = result.data.userInfo
							app.is_teacher = result.data.userInfo.is_teacher
							resolve(app.userInfo)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 获取账户资产
			getUserAssets() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : UserApi.assets({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.assets = result.data.assets
							resolve(app.assets)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 获取当前用户待处理的订单数量
			getTodoCounts() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : OrderApi.todoCounts({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.todoCounts = result.data.counts
							resolve(app.todoCounts)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 跳转到登录页
			handleLogin() {
				!this.isLogin && this.$navTo('pages/login/index')
			},

			// 跳转到修改个人信息页
			handlePersonal() {
				this.$navTo('pages/user/personal/index')
			},

			// 退出登录
			handleLogout() {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '您确定要退出登录吗?',
					success(res) {
						if (res.confirm) {
							store.dispatch('Logout', {})
								.then(result => app.onRefreshPage())
						}
					}
				})
			},

			// 跳转到钱包页面
			onTargetWallet() {
				this.$navTo('pages/wallet/index')
			},
			onTargetLog() {console.log(758)
				!this.isLogin && this.$navTo('pages/login/index')
				this.$navTo('pages/ant/exam/log')
			},
			onTargetChouchalog() {
				!this.isLogin && this.$navTo('pages/login/index')
				this.$navTo('pages/ant/teacher/log')
			},
			onTargetCert() {
				!this.isLogin && this.$navTo('pages/login/index')
				this.$navTo('pages/ant/exam/cert')
			},
			onTargetReset() {
				!this.isLogin && this.$navTo('pages/login/index')
				this.$navTo('pages/ant/teacher/reset')
			},

			// 跳转到订单页
			onTargetOrder(item) {
				this.$navTo('pages/order/index', {
					dataType: item.id
				})
			},

			// 跳转到我的积分页面
			onTargetPoints() {
				this.$navTo('pages/points/log')
			},

			// 跳转到我的优惠券页
			onTargetMyCoupon() {
				this.$navTo('pages/my-coupon/index')
			},

			// 跳转到服务页面
			handleService({
				url
			}) {
				this.$navTo(url)
			},

		},

		/**
		 * 下拉刷新
		 */
		onPullDownRefresh() {
			// 获取首页数据
			this.getPageData(() => {
				uni.stopPullDownRefresh()
			})
		},


	}
</script>

<style lang="scss" scoped>
	// 页面头部
	.main-header {
		//background: linear-gradient(180deg, rgba(225,239,255,1) 0%,rgba(255,255,255,0.88) 97%);
		background: linear-gradient(to bottom, #E1EFFF, rgba(225, 239, 255, 0.5));


		position: relative;
		width: 100%;

		background-size: 100% 100%;
		overflow: hidden;
		display: flex;
		align-items: center;

		padding-left: 60rpx;

		.bg-image {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 0;
		}

		.user-info {
			display: flex;
			height: 100rpx;
			z-index: 1;

			.user-content {
				display: flex;
				flex-direction: column;
				justify-content: center;
				margin-left: 30rpx;
				color: rgba(0, 0, 0, 1);

				.nick-name {
					font-size: 28rpx;
					font-weight: bold;
					max-width: 270rpx;
				}

				.mobile {
					margin-top: 15rpx;
					font-size: 24rpx;
				}

				.user-grade {
					align-self: baseline;
					display: flex;
					align-items: center;
					background: #3c3c3c;
					margin-top: 12rpx;
					border-radius: 10rpx;
					padding: 4rpx 12rpx;

					.user-grade_icon .image {
						display: block;
						width: 32rpx;
						height: 32rpx;
					}

					.user-grade_name {
						margin-left: 5rpx;
						font-size: 26rpx;
						color: #EEE0C3;
					}

				}

				.login-tips {
					margin-top: 12rpx;
					font-size: 24rpx;
				}

			}
		}
	}

	// 角标组件
	.item-badge {
		position: absolute;
		top: 0;
		right: 55rpx;
		background: #fa2209;
		color: #fff;
		border-radius: 100%;
		min-width: 38rpx;
		height: 38rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 1rpx;
		font-size: 24rpx;
	}

	// 我的钱包
	.my-asset {
		display: flex;
		background: #fff;
		padding: 40rpx 0;

		.asset-right {
			width: 200rpx;
			border-left: 1rpx solid #eee;
		}

		.asset-right-item {
			text-align: center;
			color: #545454;

			.item-icon {
				font-size: 44rpx;
			}

			.item-name {
				margin-top: 14rpx;
				font-size: 28rpx;
			}

		}

		.asset-left-item {
			max-width: 183rpx;
			text-align: center;
			color: #666;
			padding: 0 16rpx;

			.item-value {
				font-size: 34rpx;
				color: red;
			}

			.item-name {
				margin-top: 6rpx;
			}

			.item-name {
				margin-top: 14rpx;
				font-size: 28rpx;
			}
		}

	}

	// 订单操作
	.order-navbar {
		display: flex;
		margin: 20rpx auto 20rpx auto;
		padding: 20rpx 0;
		width: 94%;
		height: 320rpx;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		font-size: 30rpx;
		border-radius: 10rpx;
		background-color: rgba(245, 245, 245, 1);

		&-item {
			position: relative;
			width: 25%;

			.item-icon {
				text-align: center;
				margin: 0 auto;
				padding: 0rpx 0;
				color: #545454;
				font-size: 44rpx;
			}

			.item-icon image {
				width: 62rpx;
				height: 62rpx;
			}

			.item-name {
				font-size: 28rpx;
				color: #101010;
				text-align: center;
				margin-right: 10rpx;
			}

		}
	}

	// 我的服务
	.my-service {
		margin: 22rpx auto 22rpx auto;
		padding: 22rpx 0;
		width: 94%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 5rpx;
		background: #fff;

		.service-title {
			padding-left: 24rpx;
			margin-bottom: 20rpx;
			font-size: 30rpx;
		}

		.service-content {

			margin-bottom: -20rpx;

			.service-item {
				position: relative;
				width: 25%;
				float: left;
				margin-bottom: 30rpx;

				.item-icon {
					text-align: center;
					margin: 0 auto;
					padding: 14rpx 0;
					color: #ff3800;
					font-size: 44rpx;
				}

				.item-name {
					font-size: 28rpx;
					color: #545454;
					text-align: center;
					margin-right: 10rpx;
				}

			}
		}
	}

	// 退出登录
	.my-logout {
		display: flex;
		justify-content: center;
		margin-top: 50rpx;

		.logout-btn {
			width: 60%;
			margin: 0 auto;
			font-size: 28rpx;
			color: #616161;
			border-radius: 20rpx;
			border: 1px solid #dcdcdc;
			padding: 16rpx 0;
			text-align: center;
		}
	}
</style>